package com.right.health.app.ui.common;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.UpdateManager;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.constants.AppConfig;
import com.right.health.app.db.DaoSharedPreferences;
import com.right.health.app.hyphenate.DemoHelper;
import com.right.health.app.ui.main.MainActivity;
import com.right.health.app.ui.user.LoginActivity;

/*
 * Created by hopeliao on 2019-12-27
 *
 */public class WelcomeActivity extends BaseActivity {

    // 延迟启动
    private static final long DELAY_MILLIS = 1000;

    private Handler mHandler;

    private int maxWait = 3;  //对于自动登陆要适当延长等待时间 也就是maxWait*DELAY_MILLIS

    private UpdateManager.Version mVersion;


    @Override
    public void onCreate(Bundle savedInstanceState, String tag) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.welcome);
    }

    @Override
    public void findView() {
    }

    @Override
    public void initialize() {
        DemoHelper.getInstance().initHandler(this.getMainLooper());
        if (DemoHelper.getInstance().isLoggedIn()) {
            EMClient.getInstance().chatManager().loadAllConversations();
            EMClient.getInstance().groupManager().loadAllGroups();
        }
        boolean isFirst = DaoSharedPreferences.getInstance().getIsFirstOpen();
        if (isFirst) {
            DaoSharedPreferences.getInstance().setIsFirstOpen(false);
        }
        if (!DaoSharedPreferences.getInstance().isAgreeProtocol()) {
            showProtocolDialog();
        } else {
            mHandler = new Handler();
            mHandler.postDelayed(runnable, DELAY_MILLIS);
        }
    }

    private void showProtocolDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        SpannableString spannableString = getSpannableString();
        TextView textView = new TextView(this);
        textView.setText(spannableString);
        textView.setTextColor(getResources().getColor(R.color.color_999999));
        int padding = getResources().getDimensionPixelSize(R.dimen.dp_14);
        textView.setPadding(padding,padding,padding,padding);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        builder.setTitle("提示")
                .setView(textView)
                .setPositiveButton("同意", (dialog, which) -> {
                    DaoSharedPreferences.getInstance().setIsAgreeProtocol(true);
                    dialog.dismiss();
                    forware();
                }).setNegativeButton("拒绝", (dialog, which) -> {
                     finish();
                });
        final AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    public SpannableString getSpannableString() {
        SpannableString spannableString = new SpannableString("使用此应用需要阅读并同意《隐私协议》和《服务协议》！");
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_09C3BE)), 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(
                new ClickableSpan() {
                    @Override
                    public void onClick(View v) {
                        ArticleDetailActivity.makeActivity(WelcomeActivity.this, "隐私协议", AppConfig.HTTP_ADDRESS + "/pandWx/agreement/h5_user_privacy.html");

                    }
                }, 12, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_09C3BE)), 19, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(
                new ClickableSpan() {
                    @Override
                    public void onClick(View v) {
                        ArticleDetailActivity.makeActivity(WelcomeActivity.this, "服务协议", AppConfig.HTTP_ADDRESS + "/pandWx/agreement/h5_provisions_declare.html");
                    }
                }, 19, 25, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    private Runnable runnable = new Runnable() {

        @Override
        public void run() {

            forware();
        }

    };

    private void forware() {
        if (!App.getInstance().isLogined()) {
            startIntent(LoginActivity.class);
        } else {
            startIntent(MainActivity.class);
        }
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 清除这个回调 防止内存泄漏
        if (mHandler != null) {
            mHandler.removeCallbacks(runnable);
        }
    }
}
